﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcAtletIS.Models;

namespace MvcAtletIS.Areas.Admin.Controllers
{
    [Authorize(Roles = "Administrators")]
    public class CheckPointsController : Controller
    {
        private atletisEntities db = new atletisEntities();

        // GET: /CheckPoints/
        public ViewResult Index(int disciplineListId)
        {
            var checkpoints = db.CheckPoints.Where(c => c.DisciplineListId == disciplineListId).OrderBy(c => c.OrderInDiscipline).Include("DisciplineList");
            ViewBag.DisciplineList = db.DisciplineList.Single(dl => dl.ID == disciplineListId);
            return View(checkpoints.ToList());
        }

        // GET: /CheckPoints/Details/5
        public ViewResult Details(int id)
        {
            CheckPoints checkpoints = db.CheckPoints.Single(c => c.ID == id);
            ViewBag.DisciplineList = db.DisciplineList.Single(dl => dl.ID == checkpoints.DisciplineListId);
            return View(checkpoints);
        }

        // GET: /CheckPoints/Create
        public ActionResult Create(int disciplineListId)
        {
            ViewBag.DisciplineList = db.DisciplineList.Single(dl => dl.ID == disciplineListId);

            CheckPoints model = new CheckPoints();
            model.DisciplineListId = disciplineListId;

            return View(model);
        } 

        // POST: /CheckPoints/Create
        [HttpPost]
        public ActionResult Create(CheckPoints checkpoints)
        {
            if (ModelState.IsValid)
            {
                db.CheckPoints.AddObject(checkpoints);
                db.SaveChanges();
                return RedirectToAction("Index", new { disciplineListId = checkpoints.DisciplineListId });  
            }

            ViewBag.DisciplineListId = new SelectList(db.DisciplineList, "ID", "Name", checkpoints.DisciplineListId);
            return View(checkpoints);
        }
        
        // GET: /CheckPoints/Edit/5
        public ActionResult Edit(int id)
        {
            CheckPoints checkpoints = db.CheckPoints.Single(c => c.ID == id);

            if (checkpoints == null)
            {
                return HttpNotFound();
            }

            ViewBag.DisciplineList = db.DisciplineList.Single(dl => dl.ID == checkpoints.DisciplineListId);
            return View(checkpoints);
        }

        // POST: /CheckPoints/Edit/5
        [HttpPost]
        public ActionResult Edit(CheckPoints checkpoints)
        {
            if (ModelState.IsValid)
            {
                db.CheckPoints.Attach(checkpoints);
                db.ObjectStateManager.ChangeObjectState(checkpoints, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index", new { disciplineListId = checkpoints.DisciplineListId});
            }
            ViewBag.DisciplineListId = new SelectList(db.DisciplineList, "ID", "Name", checkpoints.DisciplineListId);
            return View(checkpoints);
        }

        // GET: /CheckPoints/Delete/5
        public ActionResult Delete(int id)
        {
            CheckPoints checkpoints = db.CheckPoints.Single(c => c.ID == id);
            ViewBag.DisciplineList = db.DisciplineList.Single(dl => dl.ID == checkpoints.DisciplineListId);
            return View(checkpoints);
        }

        // POST: /CheckPoints/Delete/5
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            try
            {
                CheckPoints checkpoints = db.CheckPoints.Single(c => c.ID == id);

                if (checkpoints == null)
                {
                    return HttpNotFound();
                }

                int? disciplineListId = checkpoints.DisciplineListId;
                db.CheckPoints.DeleteObject(checkpoints);
                db.SaveChanges();
                return RedirectToAction("Index", new { disciplineListId = disciplineListId });
            }
            catch (Exception ex)
            {
                return RedirectToAction("Error", "Home", new { message = ex.Message + " " + ex.InnerException.ToString().Substring(0, 1000) });
            }
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}